from django.db import models


# Create your models here.

# 电影标签表
class MovieLabel(models.Model):
    label = models.CharField(max_length=20, verbose_name="label")

    def __str__(self):
        return str(self.label)

    class Meta:
        ordering = ('pk',)
        verbose_name = '标签'
        verbose_name_plural = verbose_name


# 电影表
class Movie(models.Model):
    name = models.CharField(max_length=50, verbose_name="电影名称")
    label = models.ManyToManyField(to=MovieLabel, verbose_name="标签")
    head_pics = models.FileField(upload_to="common/head_pics/", verbose_name="图片")
    visual_guide = models.FileField(upload_to="common/visual_guide", verbose_name="解说视频", null=True, blank=True)
    movie_url = models.URLField(verbose_name="链接", default="http://")
    subtitle = models.FileField(verbose_name="字幕", upload_to="common/vtt/", blank=True, null=True)
    validity = models.TextField(verbose_name="电影简介")
    point = models.TextField(verbose_name="简评")
    douban = models.URLField(verbose_name="豆瓣影评", default="http://")
    country = models.CharField(max_length=5, verbose_name="国家", null=True, blank=True, default="")
    pub_time = models.DateField(verbose_name="发布时间", null=True, blank=True)
    actors = models.TextField(verbose_name="演员", null=True, blank=True, default="")
    up_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")

    def up_head_pic_us3(self):
        pass

    def __str__(self):
        return str(self.name)

    class Meta:
        ordering = ('pk',)
        verbose_name = '电影'
        verbose_name_plural = verbose_name


class VisitRecord(models.Model):
    visit_times = models.IntegerField(verbose_name="访问次数", default=0)
    visit_token = models.CharField(max_length=32, verbose_name="访问token")
    update_time = models.DateTimeField(auto_now=True, verbose_name="最后访问时间", blank=True)

    def save(self, **kwargs):
        if kwargs.get("using", None) == "default":
            self.visit_times += 1
        super().save()

    def __str__(self):
        return str(self.visit_token)

    class Meta:
        ordering = ('-pk',)
        verbose_name = "访问记录"
        verbose_name_plural = verbose_name


class VisitDetail(models.Model):
    visit_ip = models.CharField(max_length=15, verbose_name="访问ip", blank=True, null=True)
    visit_date = models.DateField(auto_now_add=True, verbose_name="访问日期")
    visit_page = models.CharField(max_length=20, verbose_name="访问页面", blank=True, null=True)
    visit_source = models.CharField(max_length=20, verbose_name="访问渠道", blank=True, null=True)
    visit_times = models.IntegerField(verbose_name="访问次数", default=0)
    to_record = models.ForeignKey(to=VisitRecord, on_delete=models.CASCADE, verbose_name="所属记录", blank=True, null=True)

    def save(self, **kwargs):
        if kwargs.get("using", None) == "default":
            self.visit_times += 1
        super().save()

    def __str__(self):
        return str(self.to_record)

    class Meta:
        ordering = ('-pk',)
        verbose_name = "访问记录细节"
        verbose_name_plural = verbose_name


class MovieNew(models.Model):
    title = models.CharField(max_length=50, verbose_name="标题")
    content = models.TextField(verbose_name="内容", blank=True, null=True)
    source = models.CharField(max_length=100, verbose_name="来源", blank=True, null=True)
    up_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间", blank=True, null=True)

    def save(self, **kwargs):
        # with open()
        super().save()

    def __str__(self):
        return str(self.title)

    class Meta:
        ordering = ('-pk',)
        verbose_name = "新闻"
        verbose_name_plural = verbose_name


VALID_CHOICE = ((True, "有效"), (False, "无效"))
class FriendlyLink(models.Model):
    name = models.CharField(max_length=50, verbose_name="网站名称")
    link_url = models.URLField(verbose_name="链接", blank=True, null=True, default="https://www.dwang.top/")
    logo = models.CharField(max_length=100, verbose_name="logon/icon", blank=True, null=True)
    web_class = models.DateTimeField(auto_now_add=True, verbose_name="网站类型", blank=True, null=True)
    valid = models.BooleanField(verbose_name="是否有效", default=True, choices=VALID_CHOICE)

    def __str__(self):
        return str(self.name)

    class Meta:
        ordering = ('pk',)
        verbose_name = "友情链接"
        verbose_name_plural = verbose_name
